package com.aurora.util;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

public class OschinaScraper {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.oschina.net/news");

        List<WebElement> newsItems = driver.findElements(By.cssSelector("div.item"));

        for (int i = 0; i < 10; i++) {
            WebElement newsItem = newsItems.get(i);
            WebElement newsLink = newsItem.findElement(By.cssSelector("h3.header a"));
            String newsUrl = newsLink.getAttribute("href");
            newsLink.click();

            WebElement newsTitleElement = driver.findElement(By.cssSelector("h1.header"));
            String newsTitle = newsTitleElement.getText();

            WebElement newsContentElement = driver.findElement(By.cssSelector("div#articleContent"));
            String newsContent = newsContentElement.getAttribute("innerHTML");

            System.out.println("标题: " + newsTitle);
            System.out.println("内容: " + newsContent);
            System.out.println("原文链接: " + newsUrl);
            System.out.println("--------------------------------------------------");

            driver.navigate().back();
            newsItems = driver.findElements(By.cssSelector("div.item"));
        }

        driver.quit();
    }
}